from tools_algorithm import *


class Solution:
    def minimumSubarrayLength(self, nums: List[int], k: int) -> int:
        if k == 0:
            return 1
        l = v = 0
        c = [0] * 30

        def add(num):
            for i in range(30):
                c[i] += num >> i & 1

        def reduce(v, num):
            for i in range(30):
                c[i] -= num >> i & 1
                if (v >> i & 1) > c[i]:
                    v ^= 1 << i
            return v

        ans = inf
        for i, num in enumerate(nums):
            v |= num
            add(num)
            while v >= k:
                ans = min(ans, i - l + 1)
                v = reduce(v, nums[l])
                l += 1
        return ans if ans < inf else -1


s = Solution()
print(s.minimumSubarrayLength(nums=[1, 2, 3], k=2))
print(s.minimumSubarrayLength(nums=[2, 1, 8], k=10))
print(s.minimumSubarrayLength(nums=[1, 2], k=0))
